var	g_strTemplateItem_Fandian = '<span id="s1{6}" class="p1" title="{4}" cx="{1}" cy="{2}" name="{0}" address="{7}" filtertext="{0}|{7}">{6}. <b><a href="#" onclick="JumpTo(1, {6});">{0}</a></b>({1},{2}){5}{3}</span><hr/>';

function showFloat(f)
{
	return Math.floor(f*100000)/100000;
}

//chy-check 2013-08-20 从字符串获取数据集
function sGetDataSetFromString(str)
{
	var oDataSet = [];
	
	var strs = str.split(/[\r\n]+/g);
	if(strs.length<1)
		return oDataSet;

	var tmp_str,tmp_data;	
	var r = /[,，|\s]+/g;
	for(var i=0;i<strs.length;i++)
	{
		tmp_str = strs[i].split(r);
		if(tmp_str.length<2)
			continue;

		tmp_data = new Array(5);
		tmp_data[0] = oDataSet.length;
		tmp_data[3] = getFloat(tmp_str[0]);
		tmp_data[4] = getFloat(tmp_str[1]);
		
		if(tmp_str.length>=3)
			tmp_data[1] = tmp_str[2];
		else
			tmp_data[1] = "";
		
		if(tmp_str.length>=4)
			tmp_data[2] = tmp_str[3];
		else
			tmp_data[2] = "";

		oDataSet.push(tmp_data);		
	}

	return oDataSet;
}

function sTestAddFandianFromDataSet(oDataSet)
{
	if(oDataSet.length<=0)
		return;
		
	m_oListView.Clear();		
	if(m_oAllMarks && m_oAllMarks.length>0)
		m_oMap.removeOverlays(m_oAllMarks);
	
	var	obj;
	var strCur;
	var oMark;
	var oMarks = new Array();
	for(var i=0; i<oDataSet.length; i++)
	{
		obj = oDataSet[i];
		oMark = addFandianMark("fandian_"+obj[0], [obj[3], obj[4]], obj[1], obj[2]);
		oMarks.push(oMark);
		strCur = g_strTemplateItem_Fandian.format
					(
						obj[1], 
						showFloat(obj[3]), 
						showFloat(obj[4]), 
						"", 
						showFloat(obj[3])+":"+showFloat(obj[4]),
						"<br/>地址:"+obj[2],
						i+1,
						obj[2]
					);
		oItem = m_oListView.AddListItem();
		oItem.SetText(strCur);
		oItem.m_oItemData = obj[0];
	}
	m_oMap.addOverlays(oMarks,true); 
	m_oAllMarks = oMarks;	
}

function sTestAddFandianFromDataString(str)
{
	var oDataSet = sGetDataSetFromString(str);
	sTestAddFandianFromDataSet(oDataSet);
}
	
function sTestAddFandian(strQuxian)
{	
	var oDataSet = fandian_coords[strQuxian];
	sTestAddFandianFromDataSet(oDataSet);
}

function filter_change()
{
	var strFilter = $("#txtFilter").val();

	var nCount = m_oListView.GetCount();
	var strCur;
	var	oItem;
	var oElement;
	var	bVisible;
	for(var i=0; i<nCount; i++)
	{
		//-判断是否显示-
		bVisible = true;
		oItem = m_oListView.GetListItem(i);
		oElement = oItem.m_oElement;
		//strCur = $(oElement).text();
		strCur = $(oElement).children().attr("filtertext");
		if(strFilter!="" && strCur.indexOf(strFilter)<0)
			bVisible = false;

		//-列表是否显示-
		bVisible ? $(oElement).show():$(oElement).hide();

		//-图标是否显示-
		if(oItem.m_oItemData && oItem.m_oItemData>0)
		{
			m_oMap.setOverlayVisibleById("fandian_"+oItem.m_oItemData, bVisible);
		}
	}
}

function showCurFandian(bCheck)
{
	//记录缩放后的中心点和地图范围
	var bounds = m_oMap.getLngLatBounds();
	var sw = bounds.southWest;	//(min)
	var en = bounds.northEast;	//(MAX)

	var xmin = getDecodeCoordinate(sw.lngX);
	var xmax = getDecodeCoordinate(en.lngX);
	var ymin = getDecodeCoordinate(sw.latY);
	var ymax = getDecodeCoordinate(en.latY);

	var strFilter = $("#txtFilter").val();

	var nCount = m_oListView.GetCount();
	var strCur;
	var	oItem;
	var oElement;
	var	bVisible;
	var oMark,x,y;
	for(var i=0; i<nCount; i++)
	{
		//-判断是否显示-
		bVisible = true;
		oItem = m_oListView.GetListItem(i);
		oElement = oItem.m_oElement;
		//strCur = $(oElement).text();
		strCur = $(oElement).children().attr("filtertext");
		if(strFilter!="" && strCur.indexOf(strFilter)<0)
			bVisible = false;
		if(bVisible && bCheck)
		{
			if(oItem.m_oItemData && oItem.m_oItemData>0)
			{
				oMark = m_oMap.getOverlayById("fandian_"+oItem.m_oItemData);
				x = getDecodeCoordinate(oMark.lnglat.lngX);
				y = getDecodeCoordinate(oMark.lnglat.latY);
				if(!(x>=xmin && x<=xmax && y>=ymin && y<=ymax))
					bVisible = false;
			}
		}
		//-列表是否显示-
		bVisible ? $(oElement).show():$(oElement).hide();

		//-图标是否显示-
		if(oItem.m_oItemData && oItem.m_oItemData>0)
		{
			m_oMap.setOverlayVisibleById("fandian_"+oItem.m_oItemData, bVisible);
		}
	}
}

function resetFandian(event)
{
	var bCheck = document.getElementById("chkTongbu").checked;
	if (!bCheck)
		return;

	showCurFandian(bCheck);
}

$(document).ready(function () 
{
	$("a[id^='spanMarkAdd']").bind("click", function()
		{
			var keyword = $(this).attr("id").replace(/spanMarkAdd/, "").toLowerCase();
			sTestAddFandian(keyword);
		}); 

	$("#txtFilter").change(filter_change);
	$("#txtFilter").bind("keyup", filter_change);
	$("#chkTongbu").bind("click", function()
		{
			var bCheck = document.getElementById("chkTongbu").checked;
			showCurFandian(bCheck);			
		});

	m_oMap.addEventListener(m_oMap,	MConstants.MAP_MOVE_END,	resetFandian); //拖拽地图结束时触发该事件。
	m_oMap.addEventListener(m_oMap,	MConstants.ZOOM_END,		resetFandian); //缩放地图结束时触发该事件。 

	//初始化菜单
	tocity_onchange("北京");
});